<template>
	<!-- 输出设置 -->
	<div class="page-wrap">
		<div class="page-content p-0">
			<div class="page-column p-0">
				<div class="page-form p-x-0">
					<el-form
						:model="ruleForm"
						:rules="rules"
						label-position="left"
						label-width="154px"
						ref="form"
						label-suffix="："
					>
						<el-row>
							<el-col :span="24">
								<el-form-item label="输出debug等级" prop="OCIDEBUG001">
									<el-input
										size="medium"
										class="mediumForm"
										@blur="clickBlur"
										v-model.number="ruleForm.OCIDEBUG001"
									></el-input>
								</el-form-item>
								<el-form-item label="输出核素" prop="OCNUCOUT001">
									<select-para
										:disabled="
											this.ruleForm.OCIDEBUG001 <= 0
										"
										size="medium"
										class="mediumForm"
										v-model="ruleForm.OCNUCOUT001"
										:label.sync="ruleForm.OCNUCOUT001"
										:options="OCNUCOUT001options"
										@change="OCNUCOUT001SELECT"
									></select-para>
								</el-form-item>
								<el-form-item label="输出结果数量" prop="TYPE0NUMBER">
									<el-input
										size="medium"
										class="mediumForm"
										@input="TYPE0NUMBERinput"
										v-model.number="ruleForm.TYPE0NUMBER"
									></el-input>
								</el-form-item>
								<el-table ref="multipleTable" :data="ruleForm.TYPE0NUMBERtab" border>
									<el-table-column
										prop="TYPE0OUT_YANYU"
										label="输出烟团"
										align="center"
										:style="{'height':'100px'}"
										min-width="120"
									>
										<template slot-scope="scope">
											<el-form-item
												label-width="0px"
												:rules="rules.TYPE0OUT_YANYU"
												:prop="'TYPE0NUMBERtab.'+scope.$index+'.TYPE0OUT_YANYU'"
											>
												<el-input
													minlength="1"
													maxlength="20"
													oninput="value=value.replace(/^\D*(\d*(?:\.\d{0,3})?).*$/g, '$1')"
													v-model="scope.row.TYPE0OUT_YANYU"
												></el-input>
											</el-form-item>
										</template>
									</el-table-column>
									<el-table-column prop="TYPE0OUT_DIS" label="输出距离" align="center" min-width="120">
										<template slot-scope="scope">
											<el-form-item
												label-width="0px"
												:rules="rules.TYPE0OUT_DIS"
												:prop="'TYPE0NUMBERtab.'+scope.$index+'.TYPE0OUT_DIS'"
											>
												<select-para
													style="width:100%"
													class="bigForm"
													v-model="scope.row.TYPE0OUT_DIS"
													:label.sync="ruleForm.TYPE0OUT_DISName"
													:options="TYPE0OUT_DISoptions"
												></select-para>
											</el-form-item>
										</template>
									</el-table-column>
									<el-table-column prop="TYPE0OUT_CCDF" label="是否输出CCDF" min-width="120" align="center">
										<template scope="scope">
											<el-form-item label-width="0px" :prop="'TYPE0NUMBERtab.'+scope.$index+'.TYPE0OUT_CCDF'">
												<select-para
													style="width:100%"
													class="bigForm"
													v-model="scope.row.TYPE0OUT_CCDF"
													:label.sync="scope.row.TYPE0OUT_CCDF"
													:options="TYPE0OUT_CCDFoptions"
												></select-para>
											</el-form-item>
										</template>
									</el-table-column>
								</el-table>
							</el-col>
						</el-row>
					</el-form>
				</div>
				<div class="bottom-btn">
					<el-button type="primary" :loading="loading" @click="submitFun">下一步</el-button>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
import selectPara from "@/components/selectPara"
import { v08, v0035, selecttext, dome } from "@/utils/verify.js"
import { mapState } from "vuex"
export default {
	name: "aboutUs",
	props: {
		batchid: String,
	},
	components: {
		selectPara,
	},
	data() {
		var v0035 = (rule, value, callback) => {
			if (value === "") {
				callback(new Error("输入值不能为空"))
			} else if (value < this.min || value > 35) {
				callback(new Error("输入范围为[" + this.min + "，35]"))
			} else {
				callback()
			}
		}

		var TYPE0OUT_YANYU = (rule, value, callback) => {
			if (value === "") {
				callback(new Error("输入值不能为空"))
			} else if (value < 1 || value > this.RDNUMREL001) {
				callback(new Error("输入范围为[1," + this.RDNUMREL001 + "]"))
			} else {
				callback()
			}
		}

		var TYPE0OUT_DIS = (rule, value, callback) => {
			if (value === "") {
				callback(new Error("输入值不能为空"))
			} else if (value < 1 || value > this.basic_Data.GENUMRAD001) {
				callback(
					new Error(
						"输入范围为[1," + this.basic_Data.GENUMRAD001 + "]"
					)
				)
			} else {
				callback()
			}
		}
		return {
			RDNUMREL001: "",
			GENUMRAD001: "",
			min: 0,
			activeTab: "9",
			ruleForm: {
				OCIDEBUG001: "0",
				OCNUCOUT001: "",
				TYPE0NUMBER: "1",
				TYPE0OUT_DISName: "",
				TYPE0NUMBERtab: [
					{
						TYPE0OUT_YANYU: "0",
						TYPE0OUT_DIS: "",
						TYPE0OUT_CCDF: "",
					},
				],
			},
			rules: {
				OCIDEBUG001: [
					{
						validator: dome(0, 8),
						trigger: ["blur", "change"],
					},
				],
				OCNUCOUT001: [
					{
						validator: selecttext,
						trigger: ["blur", "change"],
					},
				],
				TYPE0NUMBER: [
					{
						validator: dome(0, 35),
						trigger: ["blur", "change"],
					},
				],
				TYPE0OUT_YANYU: [
					{
						validator: TYPE0OUT_YANYU,
						trigger: ["blur", "change"],
					},
				],
				selecttext: [
					{
						validator: selecttext,
						trigger: ["blur", "change"],
					},
				],
			},
			OCNUCOUT001options: [],

			TYPE0OUT_CCDFoptions: [
				{
					label: "",
					value: "",
				},
				{
					label: "CCDF",
					value: "CCDF",
				},
			],
			loading: false,
		}
	},
	mounted() {
		this.getISOTPGRP_NAM()
		this.setData()
	},
	computed: {
		...mapState({
			basic_Data: (state) => state.token.basic_Data,
		}),
		TYPE0OUT_DISoptions: function () {
			let temGESPAEND_DISTANCE = this.basic_Data.GESPAEND_DISTANCE
				? this.basic_Data.GESPAEND_DISTANCE
				: []
			let temp = temGESPAEND_DISTANCE.map((item, index) => {
				return {
					label: item,
					value: index + 1,
				}
			})
			return temp
		},
	},
	methods: {
		setData() {
			this.ruleForm.OCIDEBUG001 = this.basic_Data.OCIDEBUG001.toString()
			this.ruleForm.OCNUCOUT001 = this.basic_Data.OCNUCOUT001.toString()
			this.ruleForm.TYPE0NUMBER = this.basic_Data.TYPE0NUMBER.toString()
			this.TYPE0NUMBERinput(this.basic_Data.TYPE0NUMBER)
		},
		clickBlur() { 
			if (this.ruleForm.OCIDEBUG001 >= 1) {
				this.min = 1
			}
		},
		OCNUCOUT001SELECT(value) { 
		},
		TYPE0NUMBERinput(value) {
			this.ruleForm.TYPE0NUMBERtab = []
			if (parseInt(value) > 0 || parseInt(value) < 35) {
				for (let index = 0; index < parseInt(value); index++) {
					this.ruleForm.TYPE0NUMBERtab.push({
						TYPE0OUT_YANYU: this.basic_Data.TYPE0OUT_YANYU[index]
							? this.basic_Data.TYPE0OUT_YANYU[index]
							: "0",
						TYPE0OUT_DIS: this.basic_Data.TYPE0OUT_DIS[index]
							? this.basic_Data.TYPE0OUT_DIS[index]
							: "0",
						TYPE0OUT_CCDF: this.basic_Data.TYPE0OUT_CCDF[index]
							? this.basic_Data.TYPE0OUT_CCDF[index]
							: "",
					})
				}
			}
		},
		getISOTPGRP_NAM() {
			this.OCNUCOUT001options = this.basic_Data.ISOTPGRP_NAM.map(
				(item) => {
					return {
						label: item,
						value: item,
					}
				}
			)
			this.RDNUMREL001 = this.basic_Data.RDNUMREL001
			this.GENUMRAD001 = this.basic_Data.GENUMRAD001
		},
		submitFun() {
			this.$refs.form.validate((valid) => {
				if (valid) { 
					let template = JSON.parse(JSON.stringify(this.basic_Data))
					template.OCIDEBUG001 = parseInt(this.ruleForm.OCIDEBUG001)
					template.OCNUCOUT001 = this.ruleForm.OCNUCOUT001
						? this.ruleForm.OCNUCOUT001
						: ""
					template.TYPE0NUMBER = parseInt(this.ruleForm.TYPE0NUMBER)
					if (template.TYPE0NUMBER > 0) {
						template.TYPE0OUT_YANYU = this.ruleForm.TYPE0NUMBERtab.map(
							(item) => {
								return parseFloat(item.TYPE0OUT_YANYU)
							}
						)
						template.TYPE0OUT_DIS = this.ruleForm.TYPE0NUMBERtab.map(
							(item) => {
								return parseFloat(item.TYPE0OUT_DIS)
							}
						)
						template.TYPE0OUT_CCDF = this.ruleForm.TYPE0NUMBERtab.map(
							(item) => {
								return item.TYPE0OUT_CCDF
							}
						)
					} else {
						template.TYPE0OUT_YANYU = []
						template.TYPE0OUT_DIS = []
						template.TYPE0OUT_CCDF = []
					} 
					template.SETPAGEINDEX = this.activeTab
					this.$store.dispatch("upData_basic_Data", template) 
					let params = {
						batchid: this.$props.batchid,
						batchinfo: "BASE",
						submitjson: JSON.stringify(template),
					}
					this.$appApi
						.BATCH_SUBMIT(params)
						.then((res) => {
							var str = res.data.substring(0, 1)
							if (str == "E") {
								let message = res.data.substring(6)
								this.$notify.error({
									title: "保存失败！",
									message: message,
									type: "success",
								})
							} else if (str == "S") {
								this.$notify({
									message: "保存成功！",
									type: "success",
								})
								this.$emit("changeActiveTab", this.activeTab)
					            this.$store.dispatch("change_basic_showTab", this.activeTab)
								if(activeTab >=9){
									this.$router.push({
										path: "/home",
									})
								}
							}
						})
						.catch((err) => {
							console.log(err)
						})
				}
			})
		},
	},
}
</script>
<style lang="scss" scoped>
.page-flex-row /deep/ .el-collapse-item__header {
	background: #f3f9fe;
}
</style>